package org.webball.communication;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import org.webball.base.interfaces.ICommunication;
import org.webball.base.interfaces.IList;
import org.webball.base.interfaces.IMessage;
import org.webball.base.interfaces.IPitch;
import org.webball.base.interfaces.ITeam;

public class Communication implements ICommunication {

	private Socket socket;
	
	private Sender sender;
	private Reciever reciever;
	
	public Communication(Socket s) throws IOException {
		socket = s;
		sender = new Sender(socket);
		reciever = new Reciever(socket);
	}
	
	public Communication(String host, int port) throws UnknownHostException, IOException {
		socket = new Socket (host, port);
		sender = new Sender(socket);
		reciever = new Reciever(socket);
	}
	
	@Override
	public void send(Object obj) {
		sender.send(obj);
	}

	@Override
	public void setPlayersList(IList list) {
		reciever.setPlayersList(list);
	}

	@Override
	public void setPitch(IPitch p) {
		reciever.setPitch(p);
	}

	@Override
	public void setMessage(IMessage m) {
		reciever.setMessage(m);
	}

	@Override
	public void setTeam(ITeam t) {
		reciever.setTeam(t);
	}

	@Override
	public void start() {
		sender.start();
		reciever.start();
	}

	@Override
	public void end() {
		sender.interrupt();
		reciever.interrupt();
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			socket = null;
		}
	}

}
